/*
 * @file gui.c GUI functions.
 *
 * @Copyright (C) 2007 Kris Marsh.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
 * Boston, MA  02110-1301, USA.
 */

#include "gui.h"
#include "guicb.h"

#define APP_TITLE "Virtual CD Deck"
#define SIZE_HEIGHT 300
#define SIZE_WIDTH 500

#define TABLE_STD_PAD 10

#define MAX_PITCH 20
#define PITCH_INCREMENT 0.1
#define PITCH_BIG_INCREMENT 1

static GtkWidget *gtkmain = NULL;
static GtkWidget *playbutton = NULL;
static GtkTextBuffer *time_buffer = NULL;


/* Enable/disable controls. Also sets the play button to "paused".
 */
void
gui_enable_controls(gboolean enable)
{
	gtk_widget_set_sensitive(GTK_WIDGET(playbutton), enable);
	gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(playbutton), FALSE);

	/* Refresh the display. */
	gui_update_display();
}

void
gui_update_display()
{
	gchar *pos = gst_get_play_pos();
	gchar *s = NULL;

	if (!time_buffer)
		return;

	if (!pos) {
		/* If no position, then file hasn't been loaded. Print generic statement. */
		s = g_strdup("Drag and drop a file here to load.");
	} else {
		/* Otherwise, add play status. */
		if (gst_is_playing() == TRUE)
			s = g_strconcat(pos, ". Playing\r", NULL);
		else
			s = g_strconcat(pos, ". Paused\r", NULL);

		g_free(pos);
	}

	/* Update the actual GUI display with "s". */
	/*g_debug(s);*/
	gtk_text_buffer_set_text(time_buffer, s, -1);
	g_free(s);

}


void
gui_init()
{
	/* Create the main window. */
	gtkmain = gtk_window_new(GTK_WINDOW_TOPLEVEL);
	gtk_window_set_title(GTK_WINDOW(gtkmain), APP_TITLE);
	gtk_window_set_default_size(GTK_WINDOW(gtkmain), SIZE_WIDTH, SIZE_HEIGHT);
	gtk_container_set_border_width(GTK_CONTAINER(gtkmain), 10);

	/* Set main window as drag-drop target. */
	static GtkTargetEntry targets[] = {
		{ "text/uri-list", 0, TARGET_URI },
		{ "text/plain", 0, TARGET_PLAIN },
		{ "STRING",     0, TARGET_STRING }
	};
	gtk_drag_dest_set(gtkmain, GTK_DEST_DEFAULT_MOTION|GTK_DEST_DEFAULT_HIGHLIGHT|GTK_DEST_DEFAULT_DROP, targets, G_N_ELEMENTS(targets), GDK_ACTION_COPY);
	gtk_signal_connect(GTK_OBJECT(gtkmain), "drag_data_received", GTK_SIGNAL_FUNC(guicb_dnd), NULL);


	/* Setup callbacks. */
	g_signal_connect ((gpointer) gtkmain, "destroy", G_CALLBACK (guicb_main_destroy), NULL);


	/* Add some buttons and more callbacks. */

	/* Add our table, where all buttons are going to be placed. */
	GtkTable *table = GTK_TABLE(gtk_table_new(5, 5, FALSE));
	gtk_container_add(GTK_CONTAINER(gtkmain), GTK_WIDGET(table));
	gtk_widget_show(GTK_WIDGET(table));

	/* Add Green play/pause button. */
	GdkColor colour, colourprelight, colouractive;

	playbutton = gtk_toggle_button_new_with_label("|> ||");
	gdk_color_parse("#aaffaa", &colour);
	gdk_color_parse("#77ff77", &colouractive);
	gdk_color_parse("#bbffbb", &colourprelight);
	gtk_widget_modify_bg(playbutton, GTK_STATE_NORMAL, &colour);
	gtk_widget_modify_bg(playbutton, GTK_STATE_ACTIVE, &colouractive);
	gtk_widget_modify_bg(playbutton, GTK_STATE_PRELIGHT, &colourprelight);
	
	g_signal_connect(G_OBJECT(playbutton), "clicked", G_CALLBACK(guicb_playbutton), NULL);

	gtk_table_attach(table, playbutton, 
		1, 2, 2, 3, /* left, right, top, bottom */
		GTK_EXPAND|GTK_FILL, GTK_EXPAND|GTK_FILL, TABLE_STD_PAD, TABLE_STD_PAD);
	gtk_widget_show(playbutton);


	/* Add Display - time, track name. play/pause. */
	GtkWidget *time_display;
	time_display = gtk_text_view_new();
	gtk_text_view_set_editable(GTK_TEXT_VIEW(time_display), FALSE);
	time_buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(time_display));
	gtk_text_buffer_set_text(time_buffer, "Drag and drop file to load.", -1);

	gtk_table_attach(table, time_display, 
		0, 2, 0, 1, /* left, right, top, bottom */
		GTK_EXPAND|GTK_FILL, GTK_EXPAND|GTK_FILL, TABLE_STD_PAD, TABLE_STD_PAD);
	gtk_widget_show(time_display);


	/* Pitch slider. */
	GtkWidget *pitch_slider;
	GtkObject *adj = gtk_adjustment_new(0 /* Default Value. */, -MAX_PITCH, MAX_PITCH, PITCH_INCREMENT, PITCH_BIG_INCREMENT, 0);
	pitch_slider = gtk_vscale_new(GTK_ADJUSTMENT(adj));

	g_signal_connect(G_OBJECT(pitch_slider), "value-changed", G_CALLBACK(guicb_pitchchange), NULL);

	gtk_table_attach(table, pitch_slider, 
		2, 3, 0, 3, /* left, right, top, bottom */
		GTK_EXPAND|GTK_FILL, GTK_EXPAND|GTK_FILL, TABLE_STD_PAD, 0);
	gtk_widget_show(pitch_slider);
	

	/* Rewind. */
	GtkWidget *rewindbutton = NULL;
	rewindbutton = gtk_button_new_from_stock(GTK_STOCK_MEDIA_PREVIOUS);

	g_signal_connect(G_OBJECT(rewindbutton), "clicked", G_CALLBACK(guicb_rewindbutton), NULL);

	gtk_table_attach(table, rewindbutton, 
		0, 1, 1, 2, /* left, right, top, bottom */
		GTK_EXPAND|GTK_FILL, GTK_EXPAND|GTK_FILL, TABLE_STD_PAD, TABLE_STD_PAD);
	gtk_widget_show(rewindbutton);

	/* Seek back. */

	GtkWidget *seek_back_button = NULL;
	seek_back_button = gtk_button_new_from_stock(GTK_STOCK_MEDIA_REWIND);

	g_signal_connect(G_OBJECT(seek_back_button), "clicked", G_CALLBACK(guicb_seek_button), GINT_TO_POINTER(-1));

	gtk_table_attach(table, seek_back_button, 
		1, 2, 1, 2, /* left, right, top, bottom */
		GTK_EXPAND|GTK_FILL, GTK_EXPAND|GTK_FILL, TABLE_STD_PAD, TABLE_STD_PAD);
	gtk_widget_show(seek_back_button);



	/* Seek forward. */


	/* If we have a file loaded, enable controls. */
	if (gst_uri_is_loaded() == TRUE)
		gui_enable_controls(TRUE);
	else
		gui_enable_controls(FALSE);
}

void
gui_show()
{
	gtk_widget_show(gtkmain);
	/*gdk_threads_enter();*/
	gtk_main();
	/*gdk_threads_leave();*/
}
